package com.hwz.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hwz.gulimall.product.entity.AttrEntity;
import com.hwz.gulimall.product.entity.ProductAttrValueEntity;
import com.hwz.gulimall.product.service.ProductAttrValueService;
import com.hwz.gulimall.product.vo.AttrRespVO;
import com.hwz.gulimall.product.vo.AttrVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.hwz.gulimall.product.service.AttrService;
import com.hwz.common.utils.PageUtils;
import com.hwz.common.utils.R;



/**
 * 商品属性
 *
 * @author hwz
 * @email 1281561921@qq.com
 * @date 2025-05-27 23:40:50
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;

    @Autowired
    private ProductAttrValueService productAttrValueService;

    @RequestMapping("/base/listforspu/{spuId}")
    public R listForSpu(@PathVariable("spuId")String spuId){

        List<ProductAttrValueEntity> list = productAttrValueService.list(new QueryWrapper<ProductAttrValueEntity>().eq("spu_id", spuId));

        return R.ok().put("data", list);

    }


    @RequestMapping("/update/{spuId}")
    @Transactional
    public R updateSpuAttrValue(@PathVariable("spuId")Long spuId, @RequestBody List<ProductAttrValueEntity> entities){

        productAttrValueService.updateSpuAttrValue(spuId,entities);

        return R.ok();

    }


    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params,"0", "base");

        return R.ok().put("page", page);
    }


    @RequestMapping("{attrType}/list/{cateLogId}")
    public R queryList(@RequestParam Map<String, Object> params, @PathVariable("cateLogId") String cateLogId, @PathVariable("attrType") String attrType){
        PageUtils page = attrService.queryPage(params, cateLogId, attrType);

        return R.ok().put("page", page);
    }



    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    public R info(@PathVariable("attrId") Long attrId){
//		AttrEntity attr = attrService.getById(attrId);

        AttrRespVO attr = attrService.getDetailById(attrId);

        return R.ok().put("attr", attr);
    }



    @RequestMapping("/infoList/{attrIds}")
    public R infoList(@PathVariable("attrIds") List<Long> attrIds){

        List<AttrEntity> list = attrService.getBaseMapper().selectList(new QueryWrapper<AttrEntity>().in("attr_id", attrIds));


        return R.ok().put("list", list);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrVO attr){
		attrService.save(attr);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrVO attr){
//		attrService.updateById(attr);
		attrService.editById(attr);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
